package com.example.common.bean;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.boot.env.YamlPropertySourceLoader;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.PropertySource;
import org.springframework.core.io.ClassPathResource;

import java.io.IOException;
import java.util.List;

/**
 * 可以在SpringBoot应用程序启动的初期阶段，对应用程序的环境属性进行修改、添加或删除
 *  org.springframework.boot.env.EnvironmentPostProcessor=\
 *  com.pack.binder.properties.PackEnvironmentPostProcessor
 */
public class PackEnvironmentPostProcessor implements EnvironmentPostProcessor {

    @Override
    public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
        YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader() ;
        List<PropertySource<?>> list;
        try {
            list = sourceLoader.load("pack", new ClassPathResource("com/pack/binder/properties/pack.yml"));
            list.forEach(propertySource -> environment.getPropertySources().addLast(propertySource)) ;
        } catch (IOException e) {
            e.printStackTrace() ;
        }
    }
}
